<?php
/*
 * @Author: your name
 * @Date: 2020-10-20 09:33:05
 * @LastEditTime: 2020-12-17 13:29:07
 * @LastEditors: Please set LastEditors
 * @Description: 虚拟规则验证器
 * @FilePath: /special/application/adminapiv3/validate/FirewallVirtualRuleValidate.php
 */
namespace  app\adminapiv3\validate;

use think\Validate;

class FirewallVirtualRuleValidate extends Validate
{
    protected $rule = [
        //源ip
        'src_ip|来源地址IP' => 'require|length:1,255',
        //目标ip
        'dest_ip|目标地址IP' => 'require|length:1,255',
        //源端口
        'src_port|来源端口' => 'require|number|between:1,65535',
        //目标端口
        'dest_port|目标端口' => 'require|number|between:1,65535',
        //是否记录 1记录 0不记录
        'have_mac_addr|是否记录MAC值' => 'require|in:0,1',
        //状态 0不启用 1启用
        'status|规则状态' => 'require|in:0,1',
        //处理方式 0拒绝 1通过
        'process|规则默认处理方式' => 'require|in:0,1',
        //方向 1出站 2入站
        'direction|流量方向' => 'require|in:1,2',
        //别名
        'alias|规则别名' => 'require|length:1,255',
        //规则名
        'rule_name|规则名' => 'require|length:1,255',
        //主机id
        'host_id|主机ID' => 'require|number|length:1,10',
        //规则主id
        'fid|规则主ID' => 'require|regex:^\d+(,\d+){0,30}'
    ];

    protected $scene = [
        //列表筛选
        // 'listFilter' => ['src_ip','dest_ip','dest_port','src_port','have_mac_addr','status','process','protocols','direction','alias'],
        //添加规则
        'add' => ['host_id','src_ip','dest_ip','dest_port','src_port','process','protocols','direction','alias'],
        //修改规则
        'modify' => ['host_id','src_ip','dest_ip','dest_port','src_port','process','protocols','direction','alias'],
        'fidRequire' => ['fid','host_id'],
    ];

    protected $message = [
        'have_mac_addr.in' => '是否记录MAC值格式错误',
        'status.in' => '规则状态格式错误',
        'process.in' => '规则默认处理方式格式错误',
        'direction.in' => '流量方向格式错误',
    ];

    //列表筛选
    public function sceneListFilter ()
    {
       return $this->only(['src_ip','dest_ip','dest_port','src_port','have_mac_addr','status','process','protocols','direction','alias'])
        ->remove('src_ip', 'require')
        ->remove('dest_ip', 'require')
        ->remove('src_port', 'require')
        ->remove('dest_port', 'require')
        ->remove('have_mac_addr', 'require')
        ->remove('status', 'require')
        ->remove('process', 'require')
        ->remove('direction', 'require')
        ->remove('alias', 'require')
        ->remove('host_id', 'require');
    }

    public function sceneLogListFilter()
    {
       return $this->only(['src_ip','dest_ip','dest_port','src_port','direction','rule_name','host_id'])
        ->remove('src_ip', 'require')
        ->remove('dest_ip', 'require')
        ->remove('src_port', 'require')
        ->remove('dest_port', 'require')
        ->remove('direction', 'require')
        ->remove('rule_name', 'require');
    }
}